package com.smile.frame.alibaba.services.user.service;

import cn.hutool.core.util.RandomUtil;
import com.smile.frame.alibaba.common.config.SnowflakeConfig;
import com.smile.frame.alibaba.common.constants.DubboConstant;
import com.smile.frame.alibaba.interfaces.user.dto.UserDTO;
import com.smile.frame.alibaba.interfaces.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LGC
 * @date 2021/12/6 17:32
 */
@Slf4j
@DubboService(version = DubboConstant.VERSION_1, retries = DubboConstant.RETRIES, timeout = DubboConstant.TIMEOUT)
public class UserServiceImpl implements IUserService {

    @Resource
    private SnowflakeConfig snowflakeConfig;

    private Map<Long, UserDTO> userMap = new HashMap<>();

    @PostConstruct
    public void init() {
        UserDTO user = null;
        long user_code;
        for (long i = 1; i <= 20; i++) {
            user = new UserDTO();
            user.setUser_id(i);
            user_code = snowflakeConfig.snowflakeId();
            user.setUser_code(user_code);
            user.setUsername("用户" + i);
            user.setMobile(RandomUtil.randomNumbers(11));
            userMap.put(i, user);
        }
    }

    @Override
    //    @SentinelResource(value = "getUserInfo")
    public UserDTO getUserInfo(Long userid) {
        log.info("用户ID:{}", userid);
        return userMap.get(userid);
    }
}
